/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers.classes;

import classes.Breadcrumbs;
import classes.HibernateUtil;
import dao.CourseDao;
import dao.PageDao;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Page;
import models.User;

/**
 * This controller is for the homepage; It collects all information for in the
 * blocks: Free lessons, Next lessons, Last updates, Newest courses.
 *
 * @author Sander
 */
public class IndexController extends HttpServlet {

    /**
     * This doGet go through all the blocks and will put all the returned
     * information in an attribute.
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HibernateUtil hu = new HibernateUtil();
        PageDao pad = new PageDao(hu.openSession());

        // Breadcrumbs
        Breadcrumbs[] breadcrumbs = {
            new Breadcrumbs("Index", "Home")
        };
        request.setAttribute("breadcrumbs", breadcrumbs);

        // Get the content from homepage
        List<Page> page = pad.findPageByTitle("Homepage");
        request.setAttribute("page", page);

        // Get the free lessons
        try {
            CourseDao cod = new CourseDao(hu.openSession());
            List free = cod.getFreeLessons();
            request.setAttribute("free", free);
        } catch (NullPointerException e) {
        }

        // Get the next lessons
        try {
            CourseDao cod = new CourseDao(hu.openSession());
            List next = cod.getNextLessons();
            request.setAttribute("next", next);
        } catch (NullPointerException e) {
        }

        // Get the last updates
        try {
            User user = (User) request.getSession().getAttribute("user");
            CourseDao cod = new CourseDao(hu.openSession());
            List updates = cod.getUpdates(user.getMember_id());
            request.setAttribute("updates", updates);
        } catch (NullPointerException e) {
        }

        // Get the newest courses
        try {
            CourseDao cod = new CourseDao(hu.openSession());
            List newest = cod.getNewestCourses();
            request.setAttribute("newest", newest);
        } catch (NullPointerException e) {
        }

        request.setAttribute("servlet", true);

        hu.close();

        RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/index.jsp");
        dispatcher.forward(request, response);
    }
}
